package com.byedu.parser;


import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;


//excel 导入
public class ExcelImportUtil {


    public static void getXlSXData(File file)
            throws FileNotFoundException, IOException {

        InputStream is = new FileInputStream(file);
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
        // 获取每一个工作薄
        for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
            XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
            if (xssfSheet == null) {
                continue;
            }

            // 获取当前工作薄的每一行,从第二航开始
            for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
                XSSFRow xssfRow = xssfSheet.getRow(rowNum);
                if (xssfRow != null) {

                    //读取第一列数据
                    XSSFCell one = xssfRow.getCell(0);
                    System.out.println(getValue(one));
                    //                System.out.println(Integer.parseInt(getValue(one)));
                    //读取第二列数据
                    XSSFCell two = xssfRow.getCell(1);
                    System.out.println(getValue(two));
                    //读取第三列数据
                    XSSFCell anwerCell1 = xssfRow.getCell(2);
                    System.out.println(getValue(anwerCell1));

                    //读取第四列数据
                    XSSFCell anwerCell2 = xssfRow.getCell(3);
                    System.out.println(getValue(anwerCell2));

                    //读取第五列数据
                    XSSFCell anwerCell3= xssfRow.getCell(4);
                    System.out.println(getValue(anwerCell3));

                    //读取第六列数据
                    XSSFCell anwerCell4 = xssfRow.getCell(5);
                    System.out.println(getValue(anwerCell4));

                    XSSFCell rightAnswer = xssfRow.getCell(6);

                    System.out.println(getValue(rightAnswer));


                    XSSFCell analysisCell = xssfRow.getCell(7);
                    System.out.println(getValue(analysisCell));


                }
            }
        }

    }
    //转换数据格式
    private static String getValue(XSSFCell xssfRow) {

        if (xssfRow.getCellType() == xssfRow.CELL_TYPE_BOOLEAN) {
            return String.valueOf(xssfRow.getBooleanCellValue());
        } else if (xssfRow.getCellType() == xssfRow.CELL_TYPE_NUMERIC) {
            return String.valueOf(xssfRow.getNumericCellValue());
        } else {
            return String.valueOf(xssfRow.getStringCellValue());
        }
    }
}